Connecting Arduino: Programming and Networking with the Ethernet Shield by Bob Hammell
Author:Bob Hammell [Hammell, Bob]
Language: eng
Format: azw3, pdf
Published: 2014-08-03T16:00:00+00:00
Communicating over UDP Sockets and Sending a DNS Request
At this stage, you only need to know about the first four bytes of the DNS message format.
Byte Type Name Description
0–1 Unsigned integer Transaction ID A 16-bit reference number that is used by the client to link DNS responses to the original request.
2–3 Unsigned integer Flags The most significant bit specifies whether the DNS message is a request (0) or a response (1).
Create a new function in the sketch:
int echo_DNS_Lookup(int sz) { int tid = random(0xFFFF); pbuf[0] = (byte)(tid >> 8); pbuf[1] = (byte)(tid & 0x0000FFFF); udp.beginPacket(dns_server, 53); udp.write(pbuf, sz); udp.endPacket(); return 0; }
The parameter sz specifies how long the DNS request is, since the allocated size of the buffer pbuf is often longer than the client’s request.
When the Arduino forwards the request to another DNS server, it needs to be able to identify when it receives the response message that matches the request. To do this, the code above changes the transaction ID of the DNS request to a random 16-bit number.
Tip: When conforming to the DNS protocol, the most-significant byte of a 16-bit integer should be sent first. The Arduino is little-endian and so the two lines that set the bytes in pbuf ensure that the two halves of the random transaction ID are divided correctly.
After a call to the EthernetUDP class method begin(), you can begin writing DNS messages to the UDP port. You do not need to open a connection to a server. Instead, the intended recipient of the message is specified in the message itself.
This code uses three methods of the EthernetUDP class:
Method Description
beginPacket() Begins a UDP message, sending the IP address of the intended recipient, and the port number.
write() Writes a series of bytes to the UDP message. The second parameter is the number of bytes to send.
endPacket() Finish sending the UDP message.
After sending the message, the echo_DNS_Lookup() function should wait for a DNS response message that has the same transaction ID. Add the following code before the line return 0; in echo_DNS_Lookup():
long timeout = millis() + 1000; while (true) { int result = udp.parsePacket(); if (result > 0) { udp.read(pbuf, sizeof(pbuf)); if ( (pbuf[2] & 0x80) == 0x80) && (pbuf[0] == ((byte)(tid >> 8))) && (pbuf[1] == ((byte)(tid & 0x0000FFFF))) ) return result; if (millis() > timeout) return 0; } delay(10); }
To ensure that only DNS response messages are processed, this code checks whether bit 15 of the message flags is set. If it is, the message is a DNS response and the loop can end.
parsePacket() does not return until a UDP message is found, and so the timeout that is implemented here is a little crude, but functional.
When the echo_DNS_Lookup() function exits, it either returns 0 to tell the calling function that no response was received from the DNS server, or it returns the length of the response. The response is in the buffer array pbuf – this overwrites the client’s original DNS request, but at this point that is no longer needed.
Download
Connecting Arduino: Programming and Networking with the Ethernet Shield by Bob Hammell.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(25279)
Hello! Python by Anthony Briggs(24330)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(23419)
Kotlin in Action by Dmitry Jemerov(22500)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(21953)
Dependency Injection in .NET by Mark Seemann(21835)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(20697)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(19514)
Grails in Action by Glen Smith Peter Ledbrook(18592)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17028)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(15836)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(13683)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(11846)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11149)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10619)
Hit Refresh by Satya Nadella(9185)
The Kubernetes Operator Framework Book by Michael Dame(8560)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8398)
Robo-Advisor with Python by Aki Ranin(8345)